<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="映射類型" prop="mapType">

        <el-select
          v-model="queryParams.mapType"
          placeholder="请選擇映射類型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.subject_map_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="业务科目" prop="busSubject">
        
        <el-select
          v-model="queryParams.busSubject"
          placeholder="请選擇业务科目"
          clearable
        >
          <el-option
            v-for="dict in dict.type.bus_subject_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="會計科目(借)" prop="accountSubject">
        <el-cascader
          filterable
          :options="accountSubjectArr"
          :props="{
            expandTrigger: 'hover',
            value: 'dictValue',
            label: 'name',
            children: 'children',
          }"
          @change="queryASHandleChange"
          clearable
        ></el-cascader>
      </el-form-item>

      <el-form-item label="會計科目(貸)" prop="accountSubjectD">
        <el-cascader
          filterable
          :options="accountSubjectArr"
          :props="{
            expandTrigger: 'hover',
            value: 'dictValue',
            label: 'name',
            children: 'children',
          }"
          @change="queryASHandleChangeD"
          clearable
        ></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['biz:subjectMap:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:subjectMap:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:subjectMap:remove']"
        >删除</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['biz:subjectMap:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="subjectMapList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="" align="center" prop="id" /> -->
      <el-table-column label="映射类型" align="center" prop="mapType" >
      <template slot-scope="scope">
          <dict-tag
            :options="dict.type.subject_map_type"
            :value="scope.row.mapType"
          />
        </template>
      </el-table-column>
      <el-table-column label="业务科目类型" align="center" prop="busSubjectType" >
      <template slot-scope="scope">
          <dict-tag
            :options="dict.type.bus_subject_type"
            :value="scope.row.busSubjectType"
          />
        </template>
      </el-table-column>
      
      <el-table-column label="业务科目" align="center" prop="busSubject" >
      
      <template slot-scope="scope">
          <dict-tag
            :options="dict.type.cost_scope"
            :value="scope.row.busSubject"
          />
        </template>
      </el-table-column>
      <el-table-column label="科目分類(借)" align="center" prop="jAccountSubjectType" >
      <template slot-scope="scope">
          <dict-tag
            :options="dict.type.as_zero_0000"
            :value="scope.row.jAccountSubjectType"
          />
        </template>
      </el-table-column>
      <el-table-column label="一級科目(借)" align="center" prop="jAccountSubjectOne" >
      <template slot-scope="scope">
          <dict-tag :options="dict_one" :value="scope.row.jAccountSubjectOne" />
        </template>
      </el-table-column>
      <el-table-column label="二級科目(借)" align="center" prop="jAccountSubjectTwo" >
      <template slot-scope="scope">
          <dict-tag :options="dict_two" :value="scope.row.jAccountSubjectTwo" />
        </template>
      </el-table-column>
      <el-table-column label="科目分類(貸)" align="center" prop="dAccountSubjectType" >
      <template slot-scope="scope">
          <dict-tag
            :options="dict.type.as_zero_0000"
            :value="scope.row.dAccountSubjectType"
          />
        </template>
      </el-table-column>
      <el-table-column label="一科目(貸)" align="center" prop="dAccountSubjectOne" >
      <template slot-scope="scope">
          <dict-tag :options="dict_one" :value="scope.row.dAccountSubjectOne" />
        </template>
      </el-table-column>
      <el-table-column label="二級科目(貸)" align="center" prop="dAccountSubjectTwo" >
      <template slot-scope="scope">
          <dict-tag :options="dict_two" :value="scope.row.dAccountSubjectTwo" />
        </template>
      </el-table-column>
      <el-table-column label="備註" align="center" prop="remark" />
      <el-table-column label="操作"      width="160"
        fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['biz:subjectMap:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['biz:subjectMap:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            >詳情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改业务科目与会计科目映射对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="业务科目" prop="busSubject">
          <el-input v-model="form.busSubject" placeholder="请输入业务科目" />
        </el-form-item>
        <el-form-item label="一級科目" prop="jAccountSubjectOne">
          <el-input v-model="form.jAccountSubjectOne" placeholder="请输入一級科目" />
        </el-form-item>
        <el-form-item label="二級科目" prop="jAccountSubjectTwo">
          <el-input v-model="form.jAccountSubjectTwo" placeholder="请输入二級科目" />
        </el-form-item>
        <el-form-item label="一級科目" prop="dAccountSubjectOne">
          <el-input v-model="form.dAccountSubjectOne" placeholder="请输入一級科目" />
        </el-form-item>
        <el-form-item label="二級科目" prop="dAccountSubjectTwo">
          <el-input v-model="form.dAccountSubjectTwo" placeholder="请输入二級科目" />
        </el-form-item>
        <el-form-item label="" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">確 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSubjectMap, getSubjectMap, delSubjectMap, addSubjectMap, updateSubjectMap } from "@/api/biz/subjectMap";
import {
  getAccountSubjectTree,
} from "@/api/biz/entry";
export default {
  name: "SubjectMap",
  dicts: [
    "subject_map_type",
    "cost_scope",
    "bus_subject_type",
    "as_zero_0000",
    "as_one_1000",
    "as_one_2000",
    "as_one_3000",
    "as_one_4000",
    "as_one_5000",
    "as_one_6000",
    "as_two_1002",
    "as_two_2241",

    
  ],
  // subject_map_type
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 顯示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 业务科目与会计科目映射表格数据
      subjectMapList: [],
      // 弹出层标题
      title: "",
      // 是否顯示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mapType: null,
        busSubjectType: null,
        busSubject: null,
        jAccountSubjectType: null,
        jAccountSubjectOne: null,
        jAccountSubjectTwo: null,
        dAccountSubjectType: null,
        dAccountSubjectOne: null,
        dAccountSubjectTwo: null,
        orderByColumn: "update_time",
        isAsc: "desc",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      dict_one: [],
      dict_two: [],
      accountSubjectArr: [],
    };
  },
  created() {
    this.getList();
    this.getAccountSubjectTree();
  },
  methods: {
    getAccountSubjectTree() {
      this.loading = true;
      getAccountSubjectTree().then((response) => {
        this.accountSubjectArr = response.data;
      });
    },
    queryASHandleChange(subject) {
      console.log(subject);
      if (subject.length === 0) {
        this.queryParams.jAccountSubjectType = "";
        this.queryParams.jAccountSubjectOne = "";
        this.queryParams.jAccountSubjectTwo = "";
      }
      if (subject.length === 1) {
        this.queryParams.jAccountSubjectType = subject[0];
      }
      if (subject.length === 2) {
        this.queryParams.jAccountSubjectType = subject[0];
        this.queryParams.jAccountSubjectOne = subject[1];
      }
      if (subject.length === 3) {
        this.queryParams.jAccountSubjectType = subject[0];
        this.queryParams.jAccountSubjectOne = subject[1];
        this.queryParams.jAccountSubjectTwo = subject[2];
      }
    },
    queryASHandleChangeD(subject) {
      console.log(subject);
      if (subject.length === 0) {
        this.queryParams.dAccountSubjectType = "";
        this.queryParams.dAccountSubjectOne = "";
        this.queryParams.dAccountSubjectTwo = "";
      }
      if (subject.length === 1) {
        this.queryParams.dAccountSubjectType = subject[0];
      }
      if (subject.length === 2) {
        this.queryParams.jAccountSubjectType = subject[0];
        this.queryParams.dAccountSubjectOne = subject[1];
      }
      if (subject.length === 3) {
        this.queryParams.dAccountSubjectType = subject[0];
        this.queryParams.dAccountSubjectOne = subject[1];
        this.queryParams.dAccountSubjectTwo = subject[2];
      }
    },
    /** 查询业务科目与会计科目映射列表 */
    getList() {
      this.loading = true;
      listSubjectMap(this.queryParams).then(response => {
        this.subjectMapList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.dict_one = [
          ...this.dict.type.as_one_1000,
          ...this.dict.type.as_one_2000,
          ...this.dict.type.as_one_3000,
          ...this.dict.type.as_one_4000,
          ...this.dict.type.as_one_5000,
          ...this.dict.type.as_one_6000,
        ];
        this.dict_two = [
          ...this.dict.type.as_two_1002,
          ...this.dict.type.as_two_2241,
        ];

      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        mapType: null,
        busSubjectType: null,
        busSubject: null,
        jAccountSubjectType: null,
        jAccountSubjectOne: null,
        jAccountSubjectTwo: null,
        dAccountSubjectType: null,
        dAccountSubjectOne: null,
        dAccountSubjectTwo: null,
        remark: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.$router.push({ path: "/tool/subjectMap/add" });
      // this.open = true;
      // this.title = "添加业务科目与会计科目映射";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.$router.push({ path: "/tool/subjectMap/edit", query: { id: id } });
      // getSubjectMap(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改业务科目与会计科目映射";
      // });
    },
    handleDetail(row) {
      const id = row.id || this.ids
      this.$router.push({ path: "/tool/subjectMap/detail", query: { id: id } });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSubjectMap(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSubjectMap(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否確认删除业务科目与会计科目映射编号為"' + ids + '"的数据项？').then(function() {
        return delSubjectMap(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('biz/subjectMap/export', {
        ...this.queryParams
      }, `subjectMap_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
